﻿/**
* userCtrl - AngularJS module for right sidebar.
*
*
* Credits
* =======
*
* Copyright 2015 LonghronShen <LonghronShen@Hotmail.com>
*/


(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'app';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.controller('orderCtrl', ['$scope', '$ngBootbox', '$stateParams', 'dataService', '$state', function ($scope, $ngBootbox, $stateParams,dataService, $state) {
        $scope.searchingState = "";
        $scope.searchingStartingPlaceCode='';
        $scope.searchingDestinationCode='';
        $scope.searchingCarLength = "";

        var province = $scope.province = "";//所属省份
        var city = $scope.city = "";//所属城市
        var area = $scope.area = "";//所属区

        $scope.provinceDestination = "";
        $scope.cityDestination = "";
        $scope.areaDestination = "";

        $scope.provinceList = "";
        $scope.cityList = "";
        $scope.areaList = "";

        $scope.provinceDestinationList = "";
        $scope.cityDestinationList = "";
        $scope.areaDestinationList = "";

        $scope.searchingStartingPlaceCode = $scope.province + $scope.city;
        $scope.searchingDestinationCode = $scope.provinceDestination + $scope.cityDestination;
        var skip = $scope.skip = 0;
        var count = $scope.count = 10;
        //状态枚举
        $scope.checkState = [{ text: 'None', value: '0' }, { text: 'Ungrabbed', value: '1' }, { text: 'Grabbed', value: '2' }, { text: 'Waiting', value: '3' }, { text: 'Shipping', value: '4' }, { text: 'Completed', value: '5' }, { text: 'Settled', value: '6' }];
        $scope.checkCarLength = [{ "text": "4.2米", "value": "1" }, { "text": "5.8米", "value": "2" }, { "text": "6米", "value": "3" }, { "text": "6.2米", "value": "4" }, { "text": "6.5米", "value": "5" }, { "text": "6.8米", "value": "6" }, { "text": "7.6米", "value": "7" }, { "text": "8.6米", "value": "8" }, { "text": "8.7米", "value": "9" }, { "text": "9.6米", "value": "10" }, { "text": "13米", "value": "11" }, { "text": "13.5米", "value": "12" }, { "text": "16米", "value": "13" }, { "text": "17.5米", "value": "14" }, { "text": "其他", "value": "15" }];
        //初始化列表
        var loadUsers = function (state, startingPlaceCode, destinationCode, carLength, skip, count) {
            dataService.GetOrderList(state, startingPlaceCode, destinationCode, carLength, skip, count).then(function (res) {
                //console.info("loadUsers: getUsers returned: ", res);
                //if (!(res.data.Error)) { console.info('success') } else { console.info('error able') }
                //res.data.Response.Orders = res.data.Error ? [] : res.data.Response.Orders;
                console.info("loadUsers: getUsers returned2: ", res);
                 var list = res.data.Response.Orders;
                for (var i = 0; i < list.length; i++) {
                    list[i].Ids = i + 1 + (skip * count);
                    list[i].IsFocus = (list[i].IsFocus == true) ? '是' : '否';
                    list[i].Amount = list[i].Amount + '' + list[i].AmountUnit;
                }
                $scope.gridApi.grid.options.totalItems = res.data.Response.Total;
                $scope.userList = { data: list };
                var currentPage = $scope.gridApi.grid.options.paginationCurrentPage;
                var totalPages = $scope.gridApi.pagination.getTotalPages();
                if (totalPages < currentPage) { $scope.gridApi.grid.options.paginationCurrentPage = 1; };
            }, function (rej) {
                $ngBootbox.alert(rej.response);
            });
        };

        

        //分页初始化
        var paginationOptions = {
            pageNumber: 0,
            pageSize: 10,
            currentPage: 1,
            sort: null,
            useCache: true
        };
        var _userList = $scope.userList = {
            data: [],
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "序号", field: 'Ids', enableColumnMenu: false, },
                { displayName: "Id", field: 'Id', enableColumnMenu: false },
                { displayName: "Contacts", field: 'Contacts', enableColumnMenu: false },
                { displayName: "Category", field: 'Category', enableColumnMenu: false },
                { displayName: "DestinationName", field: 'Line.DestinationName', enableColumnMenu: false },
                { displayName: "StartingPlaceName", field: 'Line.StartingPlaceName', enableColumnMenu: false },
                { displayName: "CarLength", field: 'CarLength', enableColumnMenu: false },
                { displayName: "Telephone", field: 'Telephone', enableColumnMenu: false },
                { displayName: "ContactsChatId", field: 'ContactsChatId', enableColumnMenu: false },
                { displayName: "IsFocus", field: 'IsFocus', enableColumnMenu: false },
                { displayName: "Amount", field: 'Amount', enableColumnMenu: false },
                { displayName: "DeliveryTime", field: 'DeliveryTime', enableColumnMenu: false },
                { displayName: "ArrivalTime", field: 'ArrivalTime', enableColumnMenu: false },
                {
                    name: "操作", field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: '<a   title="详情" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editOrderDetail(row.entity)"></a> <a class="fa fa-trash-o primary text-danger" title="删除" ng-click="grid.appScope.deleteOrder(row.entity.Id)"></a> ',
                    enableColumnMenu: false
                }
            ],
            onRegisterApi: function (gridApi) {
                $scope.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                    paginationOptions.pageNumber = newPage * pageSize - pageSize;
                    paginationOptions.pageSize = pageSize;
                    paginationOptions.currentPage = newPage;
                    $scope.gridApi.grid.options.paginationCurrentPage = newPage;
                    $scope.skip = paginationOptions.pageNumber;
                    $scope.count = paginationOptions.pageSize;
                    loadUsers($scope.searchingState, $scope.searchingStartingPlaceCode, $scope.searchingDestinationCode, $scope.searchingCarLength,
                        parseInt(newPage - 1), paginationOptions.pageSize);
                    
                });
            }
        };
       
        loadUsers($scope.searchingState, $scope.searchingStartingPlaceCode, $scope.searchingDestinationCode, $scope.searchingCarLength,
            paginationOptions.pageNumber, paginationOptions.pageSize);
        //查询调用
        $scope.queryOrders = function () {
            paginationOptions.pageNumber = 0;
            paginationOptions.currentPage = 1;
            paginationOptions.useCache = false;
            $scope.gridApi.pagination.raise.paginationChanged(paginationOptions.currentPage, paginationOptions.pageSize);
        };
        //获取详情
        $scope.editOrderDetail = function (user) {
            console.info('test 获取详情', user.Contacts);
            $state.go("app.management.order_detail", { Id: user.Id, Contacts: user.Contacts, Amount: user.Amount, CarLength: user.CarLength, Category: user.Category, ContactsChatId: user.ContactsChatId, Telephone: user.Telephone, DeliveryTime: user.DeliveryTime, ArrivalTime: user.ArrivalTime });
        };
        //删除订单
        $scope.deleteOrder = function (Id) {
            console.info('Get Id', Id);
            $ngBootbox.confirm('是否删除该订单').then(function () {
                dataService.DeleteOrder(Id).then(function (res) {
                    console.info('Get DeleteOrder', res);
                }, function (rej) { $ngBootbox.alert(rej.response); })
            }, function () { return; });
        };
    }]);

    module.controller('orderDetailCtrl', ['$scope', '$ngBootbox','$stateParams', 'dataService', '$state', function ($scope, $ngBootbox,$stateParams, dataService, $state) {
        //获取详情
        console.info('test twice', $stateParams.Contacts);
        console.info('test twice', $stateParams);
        $scope.Contacts = $stateParams.Contacts;
        $scope.Amount = $stateParams.Amount;
        $scope.CarLength = $stateParams.CarLength;
        $scope.Category = $stateParams.Category;
        $scope.ContactsChatId = $stateParams.ContactsChatId;
        $scope.Telephone = $stateParams.Telephone;
        $scope.DeliveryTime = $stateParams.DeliveryTime;
        $scope.ArrivalTime = $stateParams.ArrivalTime;
        dataService.GetSuborderList("0fbaa410a48d4c4993f117c65392749a").then(function (res) {
            console.info('Get SubOrderList', res);
        }, function (rej) { $ngBootbox.alert(rej.response); })
        //初始化列表
        var loadUsers = function (orderId) {
            dataService.GetSuborderList(orderId).then(function (res) {
                var list = res.data.Response.Orders;
                for (var i = 0; i < list.length; i++) {
                    list[i].Ids = i + 1;
                    list[i].IsFocus = (list[i].IsFocus == true) ? '是' : '否';
                    list[i].Amount = list[i].Amount + '' + list[i].AmountUnit;
                }
                //$scope.gridApi.grid.options.totalItems = res.data.Response.Total;
                $scope.suborderList = { data: list };
                $scope.height = (($scope.suborderList.data.length * 30) + 50);
            }, function (rej) {
                $ngBootbox.alert(rej.response);
            });
        };
        var _suborderList = $scope.suborderList = {
            data: [],
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "Id", field: 'Id', enableColumnMenu: false },
                { displayName: "Contacts", field: 'Contacts', enableColumnMenu: false },
                { displayName: "Category", field: 'Category', enableColumnMenu: false },
                { displayName: "DestinationName", field: 'Line.DestinationName', enableColumnMenu: false },
                { displayName: "StartingPlaceName", field: 'Line.StartingPlaceName', enableColumnMenu: false },
                { displayName: "CarLength", field: 'CarLength', enableColumnMenu: false },
                { displayName: "Telephone", field: 'Telephone', enableColumnMenu: false },
                { displayName: "ContactsChatId", field: 'ContactsChatId', enableColumnMenu: false },
                { displayName: "IsFocus", field: 'IsFocus', enableColumnMenu: false },
                { displayName: "Amount", field: 'Amount', enableColumnMenu: false },
                { displayName: "DeliveryTime", field: 'DeliveryTime', enableColumnMenu: false },
                { displayName: "ArrivalTime", field: 'ArrivalTime', enableColumnMenu: false },
                //{
                //    name: "操作", field: '操作',
                //    cellFilter: 'checkmark',
                //    cellTemplate: '<a   title="详情" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editOrderDetail(row.entity)"></a> <a class="fa fa-trash-o primary text-danger" title="删除" ng-click="grid.appScope.deleteOrder(row.entity.Id)"></a> ',
                //    enableColumnMenu: false
                //}
            ]
        };
        loadUsers($stateParams.Id);
    }]);
})();